package com.deep.spring.stream.io1;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.FileCopyUtils;

import java.io.IOException;
import java.io.InputStream;

public class FileSystemResourceTest {

    public static void main(String[] args) throws IOException {
        String filePath = "D:/com/xttblog.txt";
//使用系统文件路径方式加载文件
        Resource res1 = new FileSystemResource(filePath);
//使用类路径方式加载文件
        Resource res2 = new ClassPathResource("conf/xttblog.txt");
        InputStream ins1 = res1.getInputStream();
        InputStream ins2 = res2.getInputStream();
//        res2.getFileName();//获取文件名
//        res1.getFile();//获取资源对应的File对象
//        res1.getInputStream();//获取文件的输入流
//        res1.createRelative(String relativePath);//在相对地址创建新文件

        EncodedResource encRes = new EncodedResource(res2, "UTF-8");
        String content = FileCopyUtils.copyToString(encRes.getReader());

        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource resources[] = resolver.getResources("classpath*://com/***/*.xml");
        for (Resource resource : resources) {
            System.out.println(resource.getDescription());
        }
    }
}
